﻿<div class="body_page">
    <div id="Menu_TableContainer"></div>
</div>

<div style="padding:5px 0; text-align:center; position:relative; top:-5px">
        <a onclick="return _openWindowAddMainMenu();" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'">Thêm Main Menu</a>
       @* <a href="#" onclick="return _openWindowAddChildMainMenu();"  class="easyui-linkbutton" data-options="iconCls:'icon-add'">Thêm Menu Chức Năng (menu con)</a>*@
    </div>

<script type="text/javascript">

    var IndexRowMenuWorking = -1;

    $(document).ready(function () {

        $('#Menu_TableContainer').datagrid({
            title: 'Danh Sách Menu',
            width: $(window).width() - 30,
            height: $(window).height() - 160,
            fitColumns: true,
            scrollbarSize: 0,
            idField: 'MMID',
            singleSelect: true,
            url: _nameSubDomain + "/Menu/mainmenuList",
            columns: [[
                { field: 'MMID', resizable: false, hidden: true },
                { field: 'TEN', resizable: false, title: 'Tên menu', sortable: true, width: getwidthcolum(60) },
                { field: 'STT', resizable: false, title: 'Thứ tự', sortable: true, width: getwidthcolum(20) },
                {
                    field: 'action',
                    title: '',
                    width: getwidthcolum(10),
                    align: 'center',
                    formatter: function (value, row, index) {
                        return '<button title="Edit Record" onclick="return _openWinDownEditMainMenu(' + row.MMID + ',' + index + ');" class="jeasyui-command-button jeasyui-edit-command-button"></button>&nbsp;<button title="Delete" onclick="return _deleteMainMenu(' + row.MMID + ',' + index + ');" class="jeasyui-command-button jeasyui-delete-command-button"></button>';
                    }
                }
            ]],
            view: detailview,
            detailFormatter: function (index, row) {
                return '<div style="padding:15px 10px; position: relative;">'
                    + '<a onclick="return _openWindowAddChildMainMenu(' + row.MMID + ', ' + index + ')" href="#" class="easyui-linkbutton l-btn l-btn-small button_on_child_menu" group="" id=""><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">Thêm menu con trên menu ' + row.TEN + '</span><span class="l-btn-icon icon-add">&nbsp;</span></span></a>'
                    +  '<table class="ddv"></table></div>';
            },
            onExpandRow: function (index, row) {
                var IndexRowParent = index;
                var ddv = $(this).datagrid('getRowDetail', index).find('table.ddv');
                ddv.datagrid({
                    title: 'Danh Sách Menu Chức Năng ' + row.TEN,
                    url: _nameSubDomain + '/Menu/child_mainmenuList?MMID=' + row.MMID,
                    fitColumns: true,
                    singleSelect: true,
                    //rownumbers:true,
                    //loadMsg: '',
                    width: $(window).width() - 90,
                    idField: 'MENUID',
                    columns: [[
                        { field: 'TEN', title: 'Tên chức năng', resizable: false, width: getwidthcolum(20), align: 'left' },
                        { field: 'CONTROLLERS', title: 'Controller', resizable: false, width: getwidthcolum(20), align: 'left' },
                        { field: 'VIEWS', title: 'Views', resizable: false, width: getwidthcolum(20), align: 'left' },
                        { field: 'STT', title: 'Thứ tự', resizable: false, width: getwidthcolum(20), align: 'left' },
                        {
                            field: 'action',
                            title: '',
                            width: getwidthcolum(10),
                            align: 'center',
                            formatter: function (value, row, index) {



                                return '<button title="Edit Record" onclick="return _openWinDownEditChildsMainMenu(' + row.MENUID + ',' + IndexRowParent + ');" class="jeasyui-command-button jeasyui-edit-command-button"></button>&nbsp;<button onclick="return _deleteChildMainMenu(' + row.MENUID + ',' + IndexRowParent + ',' + index + ');" title="Delete" class="jeasyui-command-button jeasyui-delete-command-button"></button>';
                            }
                        }

                    ]],
                    onResize: function () {
                        $('#Menu_TableContainer').datagrid('fixDetailRowHeight', index);
                    },
                    onLoadSuccess: function () {
                        setTimeout(function () {
                            $('#Menu_TableContainer').datagrid('fixDetailRowHeight', index);
                        }, 0);
                    },
                    onDblClickRow: function (index, row) {
                        IndexRowMenuWorking = index;
                        loadwindowOnContent(_nameSubDomain + "/Menu/EditChildMainMenu?MID=" + row.MENUID, "window_Edit_ChildMainMenu", "Cập thông tin menu chức năng", 350, 280);
                    }
                });
                $('#Menu_TableContainer').datagrid('fixDetailRowHeight', index);
            },
            onLoadSuccess: function () {
                if (IndexRowMenuWorking != -1) {
                    $('#Menu_TableContainer').datagrid('expandRow', IndexRowMenuWorking);
                }
            },
            onDblClickRow: function (index, row) {
                IndexRowMenuWorking = index;
                loadwindowOnContent(_nameSubDomain + "/Menu/EditMainMenu?MMID=" + row.MMID, "window_Edit_MainMenu", "Cập thông tin menu", 230, 210);
            }
        });
    });

    function _openWinDownEditMainMenu(MMID, Index) {
        IndexRowMenuWorking = Index;
        loadwindowOnContent(_nameSubDomain + "/Menu/EditMainMenu?MMID=" + MMID, "window_Edit_MainMenu", "Cập thông tin menu", 350, 150);
    }

    function _openWindowAddMainMenu() {
        IndexRowMenuWorking = -1;
        loadwindowOnContent(_nameSubDomain + "/Menu/AddMainMenu", "window_Edit_MainMenu", "Thêm thông tin menu", 350, 150);
    }

    function _deleteMainMenu(MMID, Index) {

        var rows = $('#Menu_TableContainer').datagrid('getRows');    // get current page rows
        var row = rows[Index];

        $.confirm({
            text: "Bạn chắc muốn xóa menu " + row.TEN + " !. Chú ý : tất cả các menu chức năng con cũng sẽ được xóa theo",
            confirm: function (button) {

                $('#Menu_TableContainer').mask('Menu ' + row.TEN + 'đang được xóa khởi hệ thống');

                $.get(_nameSubDomain + '/Menu/DestroyMainMenu?MMID=' + MMID, function (data) {

                    IndexRowMenuWorking = -1;
                    $('#Menu_TableContainer').datagrid('reload');


                    $('#Menu_TableContainer').unmask();
                });

            },
            cancel: function (button) {

            }
        });

        return false;
    }

    function _openWindowAddChildMainMenu(MID, Index) {
        IndexRowMenuWorking = Index;
        loadwindowOnContent(_nameSubDomain + "/Menu/AddChildMainMenu?MID=" + MID, "window_Edit_ChildMainMenu", "Thêm thông tin menu chức năng", 350, 280);
    }

    function _openWinDownEditChildsMainMenu(MID, Index) {
        IndexRowMenuWorking = Index;
        loadwindowOnContent(_nameSubDomain + "/Menu/EditChildMainMenu?MID=" + MID, "window_Edit_ChildMainMenu", "Cập thông tin menu chức năng", 350, 280);
    }

    function _deleteChildMainMenu(MID, Index, IndexChild) {

        IndexRowMenuWorking = Index;

        var ddv_Parent = $("#Menu_TableContainer").datagrid('getRowDetail', Index).find('table.ddv');

        var rows = ddv_Parent.datagrid('getRows');    // get current page rows
        var row = rows[IndexChild];

        $.confirm({
            text: "Bạn chắc muốn xóa menu chức năng " + row.TEN + "!",
            confirm: function (button) {

                $('#Menu_TableContainer').mask('Menu chức năng ' + row.TEN + 'đang được xóa khởi hệ thống');

                $.get(_nameSubDomain + '/Menu/DestroyChildMainMenu?MID=' + MID, function (data) {

                    $('#Menu_TableContainer').datagrid('reload');
                    $('#Menu_TableContainer').unmask();
                });

            },
            cancel: function (button) {

            }
        });

        return false;

    }


</script>
